package com.relatev.minecraft.TRMExample;

import com.relatev.minecraft.TRMobAPI.TRBossBar;
import com.relatev.minecraft.TRMobAPI.TRJsonConfigurable;
import com.relatev.minecraft.TRMobAPI.TRLevelMob;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class FeiSheep extends TRLevelMob implements TRBossBar,TRJsonConfigurable {
    
    public final ItemStack item;

    public FeiSheep() {
        super("&r&l沸羊羊", EntityType.SHEEP);
        item = new ItemStack(Material.DIAMOND);
        ItemMeta IM = item.getItemMeta();
        IM.setDisplayName("§c好看的钻石");
        IM.setLore(Arrays.asList("§e这里是第一行lore","§d这里是第二行lore"));
        item.setItemMeta(IM);
    }

    @Override
    public LivingEntity spawn(Location loc, int level) {
        Sheep sheep = (Sheep) super.spawn(loc, level);
        sheep.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(20 + level * 5); //设定怪物出生最大生命值为 20+等级x5
        sheep.setHealth(sheep.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); //让怪物满血
        return sheep;
    }

    @EventHandler
    public void onDamage(EntityDamageEvent event) {
        if (super.isThisMob(event.getEntity())) {
            Sheep sheep = (Sheep) event.getEntity();
            if (sheep.getHealth() <= event.getDamage()) { //如果羊羊受的是致命伤
                if (super.hasCustomMetadata(sheep, "DeathCount") == false) {
                    super.setCustomMetadata(sheep, "DeathCount", 0);
                }
                super.setCustomMetadata(sheep, "DeathCount", super.getCustomMetadata(sheep, "DeathCount").asInt() + 1); //计数器 +1
                if (super.getCustomMetadata(sheep, "DeathCount").asInt() < 3) { //如果死亡次数低于3，满血这只羊羊
                    sheep.getWorld().playEffect(sheep.getLocation(), Effect.POTION_BREAK, 0); //来个复活特效
                    sheep.setHealth(sheep.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()); //给羊羊满血
                }
            }
        }
    }

    @Override
    public BossBar getBossBar(Entity ent) {
        Sheep sheep = (Sheep) ent;
        BossBar bb = Bukkit.createBossBar(DisplayName + " &c" + sheep.getHealth() + "&r/&a" + sheep.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(), BarColor.BLUE, BarStyle.SOLID, new BarFlag[0]);
        bb.setProgress(sheep.getHealth()/sheep.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
        return bb;
    }

    @Override
    public int getVisableDistance() {
        return 32;
    }
}
